import re, string

f = open('resultsupper2.html', 'r')
f.readline()#gets rid of the first line(blank)


#open file to start writing input to. Declare variables that are used in creating input predicates
w=open('clingoinput1','w')
event="" 
course=""
title=""
unique=""
day=""
start=""
finish=""
emptyclass = 0
courses = []
def main() : 
	line = f.readline()
	i = 0
	prevcourse=""
	while line != "" :
		if "<span class=\"title\">" in line :
			cut = re.split("> ",line)	
			
			course = cut[3][0:cut[3].find("<")-1]
			course=course.replace(' ',"").lower()
		
			title = cut[4][0:cut[4].find("<")-1]
	#remove &amp; 
			title = title.replace("&amp;","")
			print title
			pattern = re.compile('[\W_]+')

			title = pattern.sub('', title)
			title=title.lower()
			
			#print course + title

			
			course+=title
		elif "unique" in line and "WBX" in line :
			unique = line[line.find("unique=")+7:line.find("unique")+12]
			unique = str(int(unique))
			
			line=f.readline()
			j=0
			
			
	
			if "<br />" in line :
				days1 = line[line.find("btx\">")+6:line.find("<br />")]
				days2 = line[line.find("<br />")+6:line.find("</p>")]
				
				line=f.readline()
				times1 = line[line.find("btx\">")+6:line.find("<br />")]
				times2 = line[line.find("<br />")+6:line.find("</p>")]
				
				starttime1=times1[0:times1.find("&")]
				finishtime1=times1[times1.rfind(";")+1:len(times1)-2]
				ampm1=times1[len(times1)-2]
				
				start=starttime1
				finish=finishtime1
		
				if "p" in ampm1 and finish != "" :
					if int(finish) >= int(start):			
						start=int(start)+1200
						finish=int(finish)+1200
					else:
						finish=int(finish)+1200
					if int(finish) >= 2400:
						start=start-1200
						finish=finish-1200

				finddays(days1,course,unique,start,finish)	

				starttime2=times2[0:times2.find("&")]
				finishtime2=times2[times2.rfind(";")+1:len(times2)-2]
				ampm2=times2[len(times2)-2]

				start=starttime2
				finish=finishtime2

				if "p" in ampm2 and finish != "" :
					if int(finish) >= int(start) :			
						start=int(start)+1200
						finish=int(finish)+1200
					else :
						finish=int(finish)+1200
					if int(finish) >= 2400:
						start=start-1200
						finish=finish-1200
				finddays(days2,course,unique,start,finish)
					
			else :

				days = line[line.find("btx\">")+6:line.find("</p>")]
				
				line= f.readline()
				times = line[line.find("btx\">")+6:line.find("</p>")]
				start=times[0:times.find("&")]
				finish=times[times.rfind(";")+1:len(times)-2]
				ampm=times[len(times)-2]
				
				if "p" in ampm and finish != "" :
						
					if int(finish) >= int(start):			
						start=int(start)+1200
						finish=int(finish)+1200
					else :
						finish=int(finish)+1200
					if int(finish) >= 2400:
						start=start-1200
						finish=finish-1200

				finddays(days,course,unique,start,finish)	
				 
			

		i+=1
		line=f.readline()
	#print courses
	#sort the courses, and write them to file
	courses.sort()	
	for course in courses :
		w.write(course+"\n")
def finddays(days, course, unique, start,finish):
	start = str(start).replace(' ',"")
	finish = str(finish).replace(' ',"")
	if "M" in days : 
		day="m" 
		event="event("+course+","+unique+","+day+","+start+","+finish+")." 
		courses.append(event)
		#w.write(event+"\n")
	if "T" in days and days.find("T") != days.find("TH") :
		day="t" 
		event="event("+course+","+unique+","+day+","+start+","+finish+")." 
		courses.append(event)
		#w.write(event+"\n")
	if "W" in days : 
		day="w" 
		event="event("+course+","+unique+","+day+","+start+","+finish+")." 
		courses.append(event)
		#w.write(event+"\n")
	if "TH" in days : 
		day="th" 
		event="event("+course+","+unique+","+day+","+start+","+finish+")." 
		courses.append(event)
		#w.write(event+"\n")
	if "F" in days : 
		day="f" 
		event="event("+course+","+unique+","+day+","+start+","+finish+")." 
		courses.append(event)
		#w.write(event+"\n")
	#maybe a ut course schedule specific thing for detecting courses without times, but meh
	if "nbsp" in days :
		day="x"
		global emptyclass
		event="event("+course+","+unique+","+day+","+str(10000+int(emptyclass))+","+str(10000+int(emptyclass))+")." 
		emptyclass+=1		
		courses.append(event)
	
main()
f.close()
w.close()
